package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.PromotionBuildsDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.PromotionBuildsEntity;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * PromotionBuilds entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .PromotionBuilds
 */

public class PromotionBuildsDAO extends HibernateDaoSupport implements PromotionBuildsDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(PromotionBuildsDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(PromotionBuildsEntity transientInstance) {
        log.debug("saving PromotionBuilds instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(PromotionBuildsEntity persistentInstance) {
        log.debug("deleting PromotionBuilds instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public PromotionBuildsEntity findById(Integer id) {
        log.debug("getting PromotionBuilds instance with id: " + id);
        try {
            PromotionBuildsEntity instance = (PromotionBuildsEntity) getHibernateTemplate()
                    .load(PromotionBuildsEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(PromotionBuildsEntity instance) {
        log.debug("finding PromotionBuilds instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding PromotionBuilds instance with property: "
                + propertyName + ", value: " + value);
        try {
            String queryString = "from PromotionBuildsEntity as model where model."
                    + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByPromotionNum(Object promotionNum) {
        return findByProperty(PROMOTION_NUM, promotionNum);
    }

    public List findByAuthor(Object author) {
        return findByProperty(AUTHOR, author);
    }

    public List findByResult(Object result) {
        return findByProperty(RESULT, result);
    }

    public List findByDuration(Object duration) {
        return findByProperty(DURATION, duration);
    }

    public List findByBuildOn(Object buildOn) {
        return findByProperty(BUILD_ON, buildOn);
    }

    public List findAll() {
        log.debug("finding all PromotionBuilds instances");
        try {
            String queryString = "from PromotionBuildsEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public PromotionBuildsEntity merge(PromotionBuildsEntity detachedInstance) {
        log.debug("merging PromotionBuilds instance");
        try {
            PromotionBuildsEntity result = (PromotionBuildsEntity) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(PromotionBuildsEntity instance) {
        log.debug("attaching dirty PromotionBuilds instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(PromotionBuildsEntity instance) {
        log.debug("attaching clean PromotionBuilds instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static PromotionBuildsDAOInterface getFromApplicationContext(
            ApplicationContext ctx) {
        return (PromotionBuildsDAOInterface) ctx.getBean("PromotionBuildsDAO");
    }
}